import sys
import os
d = os.path.dirname(__file__)
parent_path = os.path.dirname(d)
sys.path.append(parent_path)
from ulits import fstool
import datetime


class OrdCell(object):
    def __init__(self,cell_id):
        self.cell_id=cell_id

    def find_ord(self):#查询订单
        by_excel=fstool.Excel(parent_path+'/data/order_seller.xls')
        by_all_data=by_excel.read_all_data('order_seller')
        buyer_ord_lis=[]
        buyer_ord_all=[]
        for data in by_all_data:
            if self.cell_id in data:
                buyer_ord_lis.append(data[0])
        # print(buyer_ord_lis)
        if buyer_ord_lis==[]:
            return buyer_ord_lis,buyer_ord_all
        else:
            gds_excel=fstool.Excel(parent_path+'/data/order_goods.xls')
            gds_all_data=gds_excel.read_all_data('order_goods')
            buyer_ord_all.append(gds_all_data[0])
            for i in set(buyer_ord_lis):
                for j in gds_all_data:
                    if i in j:
                        buyer_ord_all.append(j)
            return buyer_ord_lis,buyer_ord_all


    def deliver(self,obj):#发货 
        buyer_ord_all=self.find_ord()[1]
        set_list=[]
        for i in buyer_ord_all:
            if obj in i:
                set_list.append(i[1])
                set_list.append(i[3])
                set_list.append(i[2])
        # print(set_list)
        store_excel=fstool.Excel(parent_path+'/data/store.xls')
        all_data=store_excel.read_all_data('store')
        goods_id=0
        goods_num=0
        for j in all_data:
            if set_list[0] in j and set_list[1] in j:
                goods_id=j[0]
                goods_num=j[2]
        goods_num=int(goods_num)-int(set_list[2])
        dict0={'ID':int(goods_id),'商品数量':str(goods_num)}
        # store_excel.write_cell_data(dict0,'store')

        gds_excel=fstool.Excel(parent_path+'/data/order_goods.xls')
        buy_excel=fstool.Excel(parent_path+'/data/order_buyer.xls')
        sell_excel=fstool.Excel(parent_path+'/data/order_seller.xls')
        dict1={'ID':obj,'买家状态':'待收货'}
        dict2={'ID':obj,'卖家状态':'待收货'}
        gds_excel.write_cell_data(dict1,'order_goods')
        gds_excel.write_cell_data(dict2,'order_goods')
        buy_excel.write_cell_data(dict1,'order_buyer')
        sell_excel.write_cell_data(dict2,'order_seller')
        #####################减库存
        print('已发货')
    def ret_cell(self,obj):#退货   
        buyer_ord_all=self.find_ord()[1]
        set_list=[]
        for i in buyer_ord_all:
            if obj in i:
                set_list.append(i[1])
                set_list.append(i[3])
                set_list.append(i[2])
        # print(set_list)
        store_excel=fstool.Excel(parent_path+'/data/store.xls')
        all_data=store_excel.read_all_data('store')
        goods_id=0
        goods_num=0
        for j in all_data:
            if set_list[0] in j and set_list[1] in j:
                goods_id=j[0]
                goods_num=j[2]
        # print(goods_id)###########
        # print(goods_num)###########
        goods_num=int(goods_num)+int(set_list[2])
        dict0={'ID':int(goods_id),'商品数量':str(goods_num)}
        store_excel.write_cell_data(dict0,'store')

        gds_excel=fstool.Excel(parent_path+'/data/order_goods.xls')
        buy_excel=fstool.Excel(parent_path+'/data/order_buyer.xls')
        sell_excel=fstool.Excel(parent_path+'/data/order_seller.xls')
        dict1={'ID':obj,'买家状态':'已退货'}
        dict2={'ID':obj,'卖家状态':'已退货'}
        gds_excel.write_cell_data(dict1,'order_goods')
        gds_excel.write_cell_data(dict2,'order_goods')
        buy_excel.write_cell_data(dict1,'order_buyer')
        sell_excel.write_cell_data(dict2,'order_seller')
        #####################加库存
        print('已退货')







def ord_sell(cell_id):
    while True:
        print("""*****************
    1.查询订单
    2.发货
    3.退货
    4.返回
    5.退出
    *****************""")
        ord_cell=OrdCell(cell_id)
        num=input('输入操作编号：').strip()
        if num=='1':
            buyer_ord_lis,buyer_ord_all=ord_cell.find_ord()
            if buyer_ord_lis==[]:
                print('您还未有顾客')
            else:
                print('您的订单有：')
                for i in buyer_ord_all[1:]:
                    print('订单ID：%s   商品名称：%s   商品数量：%s   商品单价%s   买家状态：%s   卖家状态：%s   下单时间：%s   结单时间：%s'%(i[0],i[1],i[2],i[3],i[4],i[5],i[6],i[7]))
        elif num=='2':
            for j,k in enumerate(ord_cell.find_ord()[1],0):
                print(j,k[:2])
            ret_num=input('输入要发货的订单编号：').strip()
            if ret_num.isdigit():
                ret_num=int(ret_num)
                if 0<ret_num<len(ord_cell.find_ord()[1]):
                    if ord_cell.find_ord()[1][ret_num][4]=='待发货':
                        ret_id=ord_cell.find_ord()[1][ret_num][0]
                        ord_cell.deliver(ret_id)
                    else:
                        print('您没有权限！')
                else:
                    print('编号不正确')
            else:
                print('输入错误！')
        elif num=='3':
            for j,k in enumerate(ord_cell.find_ord()[1],0):
                print(j,k[:2])
            ret_num=input('输入要处理的退货订单编号：').strip()
            if ret_num.isdigit():
                ret_num=int(ret_num)
                if 0<ret_num<len(ord_cell.find_ord()[1]):
                    if ord_cell.find_ord()[1][ret_num][5]=='待退货':
                        ret_id=ord_cell.find_ord()[1][ret_num][0]
                        ord_cell.ret_cell(ret_id)
                    else:
                        print('您没有权限！')
                else:
                    print('编号不正确')
            else:
                print('输入错误！')
        elif num=='4':
            print('返回成功！')
            break
        elif num=='5':
            exit('已退出')
        else:
            print('输入错误！')





if __name__=='__main__':
    ord_sell('09876543210')